<?php

namespace app\api\controller;

use app\admin\model\Institution AS InstitutionMdl;
use app\admin\model\Department;
use app\admin\model\User;
use app\admin\model\Member;
use app\admin\model\Image;

class Institution extends BaseController
{

    /**
     * 机构列表
     */
    public function getInstitutionList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }
        $where = [
            'townId' => $param->townId,
        ];

        $model = db('institution');
        $fields = 'id, name, imgUrl, createDate, address, addressGeo';
        $order = 'sorting ASC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = InstitutionMdl::where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关机构');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 机构详情
     */
    public function getInstitutionDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->institutionId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'institutionId 不能这空');
        }
        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }

        $id = $param->institutionId;
        $where = [
            'id' => $id
        ];
        $model = db('institution');
        $fields = 'id, name, imgUrl, createDate, address, addressGeo, phone, content';
        $item = $model->where($where)->field($fields)->find();

        if (empty($item)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关机构');
        }

        // 部门
        $department = Department::where(['institutionId' => $id])->field('id, name')->select();
        if($department){
            foreach ($department as $value) {
                $memberList = Member::where(['departmentId' => $value['id']])->field('id, name, avatar, isParty, job, userId')->select();
                $value['memberList'] = $memberList;
            }
        }
        $item['department'] = $department;

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

    /**
     * 机构及下级部门列表
     */
    public function getAllDepartments()
    {
        $param = self::getHttpParam();

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        $where = [
            'townId' => $param->townId,
        ];

        $model = new InstitutionMdl();
        $fields = 'id, name, imgUrl, createDate, address, addressGeo';
        $order = 'sorting ASC';
        $result = $model->where($where)->field($fields)->order($order)->select();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关机构');
        }

        foreach ($result as &$item) {
            $departments = Department::where('institutionId', $item['id'])->select();
            $item['departments'] = $departments;
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    /**
     * 部门及下面的人员列表
     */
    public function getAllDepartmentMembers()
    {
        $param = self::getHttpParam();

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        $where = [
            'b.townId' => $param->townId,
            'b.isDelete' => 2
        ];

        $model = new Department();
        $fields = 'a.id, a.name';
        $order = 'a.sorting ASC, a.createDate DESC';
        $list = $model->alias('a')->where($where)->join('institution b', 'a.institutionId = b.id')->field($fields)->order($order)->select();
        if (!$list) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到部门成员信息');
        }

        $data = [];
        foreach ($list as $key => $item) {
            $members = Member::where('departmentId', $item['id'])->field('id, name, avatar, userId, institutionJob')->select();
            if ($members) {
                $item['members'] = $members;
                $data[] = $item;
            }
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

}